/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pdjs.code.utils.uptranslit.core;

import java.util.StringTokenizer;

/**
 *
 * @author niranjan
 */
public class FeedHelper {
    
    private static FeedHelper instance = new FeedHelper();
    
    private FeedHelper(){
        
    }
    
    
    public static FeedHelper getInstance() {
        return instance;
    }
    
    
    /**
     * Transliterate the line of source text word by word
     * @param source
     * @return
     */
    public String transliterateLineString(String source) {
        
        StringBuffer sbuff = new StringBuffer();
        if (source != null && !"".equals(source.trim())) {            
            //First get all the words
            StringTokenizer st = new StringTokenizer(source," ");
            while(st.hasMoreTokens()) {
            
                String word = st.nextToken();   
                if (word!=null && !"".equals(word.trim())) {
                    //Now get all the underscores and hyphens
                    StringTokenizer splits = new StringTokenizer(word,"-_",true);                    
                    //Now send for transliteration
                    while (splits.hasMoreTokens()) {
                        String split = splits.nextToken();
                        if (!"-".equals(split) && !"_".equals(split)) {
                            callCore(split,sbuff);
                        } else if ("-".equals(split)){
                            sbuff.append("-"); // we need this!
                        }
                    }
                    sbuff.append(" ");
                }
            }            
        }        
        return sbuff.toString();       
    }
    
    private void callCore(String split,StringBuffer sbuff) {        
        WordTransliterator wtInstance = WordTransliterator.getInstance();
        sbuff.append(wtInstance.translitertate(split));        
    }
}
